import { Component, OnInit } from '@angular/core';
import {EmployeeList} from '../../../model/employee-list';
import {PageInfo} from '../../../model/pageInfo';
import {StorageService} from '../../../services/storage.service';
import {EmployeeListService} from '../../../services/employee-list.service';
import {NativeService} from '../../../services/native.service';
import {NbDialogService} from '@nebular/theme';
import {ImportComponent} from '../import/import.component';
import {API} from '../../../api';
import {ModifyPopupComponent} from '../modify-popup/modify-popup.component';
import {Router} from '@angular/router';


@Component({
  selector: 'ngx-employee-list',
  templateUrl: './employee-list.component.html',
  styleUrls: ['./employee-list.component.scss'],
})
export class EmployeeListComponent implements OnInit {
  page: number = 0;
  menuState: boolean;
  pageNums: number = 0;
  pageSizes: number;
  searchContent: string;
  maxPages: number;
  pageInfo: PageInfo<EmployeeList> = { };
  constructor(private storageService: StorageService, private employeeListService: EmployeeListService,
              private nativeService: NativeService, private dialogService: NbDialogService,
              private router: Router) { }

  async ngOnInit() {
    await this.loadData();
  }
  async loadData(pageNum?) {
    const param: any = {
        pageSize: 10,
        pageNum: this.pageNums,
    };
    if (this.searchContent) {
        param.s = this.searchContent;
    }
    console.log(param);
    this.pageInfo = await this.employeeListService.employeelist(param);
    console.log(this.pageInfo);
    if (this.pageInfo.page.pageNum !== null && this.pageInfo.page.pageNum !== undefined) {
        this.pageNums = this.pageInfo.page.pageNum;
        console.log(this.pageNums);
    }
    if (this.pageInfo.page.total) {
        this.pageSizes = this.pageInfo.page.total;
    }
      if (this.pageInfo.page.maxPage) {
          this.maxPages = this.pageInfo.page.maxPage;
      }
      if (this.pageInfo.list && this.pageInfo.list.length > 0) {
          this.menuState = false;
      } else {
          console.log(this.pageInfo);
          this.menuState = true;
      }

}
// 下载导入模版
    async downloadImportTemplate() {
      await this.employeeListService.downloadImportTemplate();
         this.nativeService.showSuccess('导出成功');
    }
// 导入
  import() {
    this.dialogService.open(ImportComponent, {
      context: {
        title: '导入员工信息',
        address: API.IMPORT,
      }}).onClose.subscribe((data) => {
      if (data) {
           this.ngOnInit();
      }
    });
  }
  // 修改状态
  async modify(name: string, title: string, content: string) {
    // for (const a of this.selectedUser) {
    //   this.userIdList += a + ',';
    //   console.log(this.userIdList);
    // }
    this.dialogService.open(ModifyPopupComponent, {
      context: {
        name: name,
        title: title,
        content: content,
      },
    }).onClose.subscribe(async (data) => {
      if (!data) {
        await this.loadData();
      }
    });
  }
// 删除
    Math: any;
  async delete(name?: string) {
   const data = await this.employeeListService.delete({name: name});
   console.log(data);
   if (data) {
     this.nativeService.showSuccess('删除成功');
       await this.loadData();
   }
  }

 async details(employeeList: EmployeeList) {
    this.dialogService.open(ModifyPopupComponent, {
      context: {
        employeeList: employeeList,
      },
    }).onClose.subscribe(async (data) => {
      if (!data) {
        await this.loadData();
      }
    });
  }
    focus(input) {
        input.style.borderColor = '#3366FF';
    }
    blur(input) {
        input.style.borderColor = '';
    }
    // 重置
    async reset() {
        this.searchContent = '';
        await this.ngOnInit();
    }
}
